<?php
/**
 *  数据记录
 *
 * */

namespace app\admin\controller;

class Datalogging extends Base
{
    protected $model;
    protected $attachedModel;

    protected function initialize()
    {
        parent::initialize();
        $this->model = \app\common\model\VoteLog::getInstance();
    }

    // 投票记录
    public function voteList()
    {
        $post = $this->request->param();
        $time = isset($post['time_type']) ? $post['time_type'] : 1;
        $keyword = isset($post['keyword']) ? $post['keyword'] : '';
        $timebet = isset($post['timebet']) ? $post['timebet'] : '';
        switch ($time) {
            case 2: // 昨天
                $start_time = date('Y-m-d', time());
                $end_time = date('Y-m-d', strtotime('-1 day', time()));
                break;
            case 3: // 今日
                $start_time = date('Y-m-d', strtotime('+1 day', time()));
                $end_time = date('Y-m-d', time());
                break;
            case 4: // 近3日
                $start_time = date('Y-m-d', time());
                $end_time = date('Y-m-d', strtotime('-3 day', time()));
                break;
            case 5: // 近7日
                $start_time = date('Y-m-d', time());
                $end_time = date('Y-m-d', strtotime('-7 day', time()));
                break;
        }
        $where = [];
        // 验证ip是否合法
        $ip = filter_var($keyword, FILTER_VALIDATE_IP);
        if($keyword != ''){
            if($ip === false){
                $where[] = ['a.competitor_name','like', "%{$keyword}%"];
            }else{
                $where[] = ['a.ip','=',$keyword];
            }
        }
        $where[] = ['a.isdelete','=',0];
        $where[] = ['c.isdelete','=',0];
        $order = "a.id desc";
        // 时间
        if(!empty($timebet)){
            $exprie_time = explode(' - ',$timebet);
            $end_time = $exprie_time[0];
            $start_time = date('Y-m-d', strtotime("+1 day",strtotime($exprie_time[1])));
            $where[] = ['a.create_time','between time',[$end_time, $start_time]];
        }else{
            if ($time > 1) {
                $where[] = ['a.create_time','between time',[$end_time, $start_time]];
            }
        }
        $list = $this->model->alias('a')
            ->field('a.*,b.competitor_num,b.first_image,c.title')
            ->join('zzcms_competitor b', 'b.id=a.competitor_id')
            ->join('zzcms_activity c', 'c.id=a.activity_id')
            ->where($where)
            ->order($order)
            ->paginate(10, false, ['query' => request()->param()]);
        $ip2regionObj = new \ip2region\Ip2Region('ip2region.db');
        foreach ($list as $k => &$v) {
            $v['image'] = $v['first_image'];
            $data = $ip2regionObj->binarySearch($v['ip']);
            $region = explode('|', $data['region']);
            $v['ip'] = $v['ip'] . '/' . $region[2] . ($region[3] ? $region[3] : '') . $region[4];
        }
        $this->assign("list", $list);
        $this->assign("time", $time);
        $this->assign("keyword", $keyword);
        $this->assign("timebet", $timebet);
        return $this->fetch('votelist');
    }


    public function ip()
    {
        $ip = '171.15.107.131';
        $ip2regionObj = new \ip2region\Ip2Region('ip2region.db');
        $data = $ip2regionObj->binarySearch($ip);
        $region = explode('|', $data['region']);
        echo '/' . $region[2] . ($region[3] ? $region[3] : '') . $region[4];

    }


}